<?php
class Form_Menu extends QForm
{
	function __construct($action)
	{
		// 调用父类的构造函数
		parent::__construct('form_menu', $action);

		// 从配置文件载入表单
		$filename = rtrim(dirname(__FILE__), '/\\') . DS . 'menu_form.yaml';
		$this->loadFromConfig(Helper_YAML::loadCached($filename));
		$this->addValidations(Menus::meta());
		$this['title']->addValidations(array($this,'validate_title'),'您的菜单名输入错误!');
		$this['parentid']->addValidations(array($this,'validate_parentid'),'上级菜单选择错误!');
	}
	
	/**
	 * 验证菜单名称
	 */
	function validate_title()
	{
		return Helper_Param::check($this['title']->value, 'safe');
	}
	/**
	 * 验证上级菜单选择是否正确
	 */
	function validate_parentid()
	{
		$parent_id = $this['parentid']->value;
		if ($parent_id == 0) {
			return true;
		}else{
			return (bool) Menus::find('id = ?',$parent_id)->getCount();
		}
	}
}